
package output.image;

import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * Line Chart sample code<br>
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class Save2PNGFile
{
	private String rowKey = "price";

	private String columnKey1 = "06/01";

	private String columnKey2 = "06/02";

	private String columnKey3 = "06/03";

	private String columnKey4 = "06/04";

	private String columnKey5 = "06/05";

	private String title = "個別房價指數";

	private String categoryAxisLabel = "估價時間";

	private String valueAxisLabel = "總價(萬元)";

	private PlotOrientation orientation = PlotOrientation.VERTICAL;

	public Save2PNGFile() throws IOException
	{
		JFreeChart chart = createLineChart();
		
		write( chart );
	}

	public static void main( String[] args )
	{
		try
		{
			new Save2PNGFile();
		}
		catch( IOException e )
		{
			e.printStackTrace();
		}
	}

	private JFreeChart createLineChart()
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue( 841, rowKey, columnKey1 );
		dataset.addValue( 845, rowKey, columnKey2 );
		dataset.addValue( 700, rowKey, columnKey3 );
		dataset.addValue( 910, rowKey, columnKey4 );
		dataset.addValue( 823, rowKey, columnKey5 );

		return ChartFactory.createLineChart( title, categoryAxisLabel, valueAxisLabel, dataset, orientation, false, true, false );
	}

	/**
	 * 將圖檔存到 file, width=300, height=200
	 * @param chart
	 * @throws IOException 
	 */
	private void write( JFreeChart chart ) throws IOException
	{
		File file = new File("image/SaveChart.png");
		
		ChartUtilities.saveChartAsPNG( file, chart, 400, 200 );
		
		System.out.println( "圖檔已儲存" );
	}
}
